﻿using Module.Common.Common;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using ServiceItem;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Data;
using System.Diagnostics;
using System.Linq;

namespace PrismChessApp.ViewModels
{
    public class UserQiPuViewModel : BindableBase
    {
        private DataTable _qipuData;
        public DataTable QipuData
        {
            get { return _qipuData; }
            set { SetProperty(ref _qipuData, value); }
        }
        private readonly SqliteHelper sqliteHelper;
        public DelegateCommand<string> MenuCommand { get; private set; }
        public DelegateCommand<object> DeleteCommand { get; private set; }
        public UserQiPuViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider containerProvider)
        {
            sqliteHelper = containerProvider.Resolve<SqliteHelper>();
            MenuCommand = new DelegateCommand<string>(MenuClick);
            DeleteCommand = new DelegateCommand<object>(DeleteRow);
            QipuData = sqliteHelper.Select("mybook", "rowid,*");
        }

        private void DeleteRow(object obj)
        {
            DataRow[] change = QipuData.Select();
        }

        private void MenuClick(string obj)
        {
            switch (obj)
            {
                case "Refresh": QipuData = sqliteHelper.Select("mybook", "rowid,*"); break;
                case "DeleteRow": break;
                case "SaveChange":
                    DataTable change = QipuData.GetChanges();
                    if (change != null) sqliteHelper.Update(change.TableName,"rowid", change);
                    break;
                default: break;
            }
        }
    }
}
